package com.ny.service.impl;

import com.ny.entity.Devices;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceServiceImpl implements DeviceService {
    
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Devices> findAll() {
        return deviceMapper.findAll();
    }
    
    @Override
    public List<Devices> findByName(String name) {
        return deviceMapper.findByName(name);
    }
    
    @Override
    public List<Devices> findByNameContaining(String name) {
        return deviceMapper.findByNameContaining(name);
    }
    
    @Override
    public List<Devices> findByNameContainingIgnoreCase(String name) {
        return deviceMapper.findByNameContainingIgnoreCase(name);
    }
    
    @Override
    public Devices save(Devices device) {
        return deviceMapper.save(device);
    }
    
    @Override
    public Optional<Devices> findByDeviceId(int deviceId) {
        return deviceMapper.findByDeviceId(deviceId);
    }
    
    @Override
    public Devices updateDeviceNameById(int deviceId, String name) {
        Optional<Devices> deviceOptional = deviceMapper.findByDeviceId(deviceId);
        if (deviceOptional.isPresent()) {
            Devices device = deviceOptional.get();
            device.setName(name);
            return deviceMapper.save(device);
        }
        return null;
    }
    
    @Override
    public boolean deleteByDeviceId(int deviceId) {
        Optional<Devices> deviceOptional = deviceMapper.findByDeviceId(deviceId);
        if (deviceOptional.isPresent()) {
            deviceMapper.deleteByDeviceId(deviceId);
            return true;
        }
        return false;
    }
}